﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.Data
{
    /// <summary>
    /// 栓查网格层高程值的错误
    /// </summary>
    public static class FixLayerError
    {

        //这些方法里还要加入无效值 


        /// <summary>
        /// 判断每层有多少个错误
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static List<int> GetErrorNum(List<double[,]> datas,List <int[,]> ibound,double thickness)
        {
            List<int> num = new List<int>();
            if (datas == null || datas.Count == 0) return new List<int>();

            for (int i = 0; i < datas.Count - 1; i++)  //先遍历一层
            {
                int k = 0;
                for (int m = 0; m < datas[i].GetLength(0); m++)  // x
                {
                    for (int n = 0; n < datas[i].GetLength(1); n++) // y
                    {
                        //if (Math.Round(datas[i][m, n] - datas[i + 1][m, n], 6) <= thickness)// && ibound[i][m,n]==1)
                        if ((thickness-Math.Round(datas[i][m, n] - datas[i + 1][m, n], 6)  ) > 0.00001)
                        {
                            k++;
                        }
                    }
                }
                num.Add(k);
            }
            //注意：层数是要比数据少一层的
            //num.Add(0);  //最后一层没有错误

            return num;
        }

        /// <summary>
        /// 平均法
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="limitData"></param>
        public static void Average(List<double[,]> datas, double thickness,ref List <int[,]> ibound)
        {
            for (int i = 0; i < datas.Count - 1; i++)  //先遍历一层
            {
                for (int m = 0; m < datas[i].GetLength(0); m++)  // x
                {
                    for (int n = 0; n < datas[i].GetLength(1); n++) // y
                    {
                        if (Math.Round(datas[i][m, n] - datas[i + 1][m, n], 6) < thickness)
                        {
                            double value = datas[i][m, n];
                            datas[i][m, n] = datas[i][m, n] / 2 + datas[i + 1][m, n] / 2 + thickness / 2;
                            datas[i + 1][m, n] = value / 2 + datas[i + 1][m, n] / 2 - thickness / 2;
                        }
                    }
                }
            }

            //GetIBound(datas, thickness, ref ibound);

        }

        /// <summary>
        /// 保留上一层的值
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="limitData"></param>
        public static void ModifyUp(List<double[,]> datas, double thickness, ref List<int[,]> ibound)
        {
            for (int i = 0; i < datas.Count - 1; i++)  //先遍历一层
            {
                for (int m = 0; m < datas[i].GetLength(0); m++)  // x
                {
                    for (int n = 0; n < datas[i].GetLength(1); n++) // y
                    {
                        if (Math.Round(datas[i][m, n] - datas[i + 1][m, n], 6) < thickness)
                        {
                            datas[i + 1][m, n] = datas[i][m, n] - thickness;
                        }
                    }
                }
            }

            //GetIBound(datas, thickness, ref ibound);
        }

        /// <summary>
        /// 保留下一层的值
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="limitData"></param>
        public static void ModefyBottom(List<double[,]> datas, double thickness, ref List<int[,]> ibound)
        {
            for (int i = 0; i < datas.Count - 1; i++)  //先遍历一层
            {
                for (int m = 0; m < datas[i].GetLength(0); m++)  // x
                {
                    for (int n = 0; n < datas[i].GetLength(1); n++) // y
                    {
                        if (Math.Round(datas[i][m, n] - datas[i + 1][m, n], 6) < thickness)
                        {
                            if (datas[i][m, n] < datas[i + 1][m, n])
                            {
                                datas[i][m, n] = datas[i + 1][m, n] + thickness;
                            }
                        }
                    }
                }
            }

            //GetIBound(datas, thickness, ref ibound);
        }



        /// <summary>
        /// 最底部数据错误的时候的情况
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="thickness"></param>
        public static void TruncateBedrock(List<double[,]> datas, double thickness, ref List<int[,]> ibound)
        {
            //这个方法最少需要三层

            if (datas == null || datas.Count < 3) return;

            for (int m = 0; m < datas[0].GetLength(0); m++)  // x
            {
                for (int n = 0; n < datas[0].GetLength(1); n++) // y
                {
                    for (int i = 0; i < datas.Count - 1; i++)
                    {
                        if (Math.Round(datas[i][m, n] - datas[datas.Count - 1][m, n], 6) < thickness)
                        {

                            datas[i][m, n] = datas[datas.Count - 1][m, n];
                            for (int j = i + 1; j < datas.Count; j++)
                            {
                                datas[j][m, n] = datas[j - 1][m, n] - thickness;
                            }

                            break;
                        }

                    }
                }

            }

            //GetIBound(datas, thickness, ref ibound);

        }


        public static void GetIBound(List<double[,]> datas, double thickness, ref List<int[,]> ibounds)
        {
            if (datas == null) return;
            if (datas.Count < 2) return;
            if (ibounds == null) return;
            if (ibounds.Count < 1) return;
            if (datas.Count - 1 != ibounds.Count) return;
            if (ibounds[0].GetLength(0) != datas[0].GetLength(0)) return;
            if (ibounds[0].GetLength(1) != datas[0].GetLength(1)) return;

            for (int i = 1; i < datas.Count; i++)
            {
                int[,] ibound = new int[datas[i].GetLength(0), datas[i].GetLength(1)];

                for (int m = 0; m < datas[i].GetLength(0); m++)
                {
                    for (int n = 0; n < datas[i].GetLength(1); n++)
                    {
                        if (Math.Round(datas[i - 1][m, n] - datas[i][m, n], 6) <= thickness)
                        {
                            if (ibounds[i - 1][m, n] != 0) ibounds[i - 1][m, n] = 0;
                        }
                    }
                }

            }

        }




    }
}
